home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Miscellaneous / ival / misc.c < prev    next >
Text File  |  1987-05-25  |  3KB  |  114 lines

  1. /*
  2.  * misc.c - miscellaneous routines
  3.  *
  4.  */
  5.  
  6. #include <memory.h>
  7. #include <quickdraw.h>
  8. #include <osutil.h>
  9. #include <resource.h>
  10.  
  11. /*
  12.  * PictToMap() - convert a resource picture into a bitmap containing the
  13.  *  result of drawing that picture.
  14.  * The resulting bitmap's upper-left corner is [0,0].
  15.  */
  16.  
  17. BitMap *
  18. PictToMap(pic)
  19. PicHandle pic;
  20. {
  21.     GrafPtr saveport;        /* current port (to be restored)    */
  22.     GrafPort aport;        /* a temp grafport for drawing into    */
  23.     BitMap *mapptr;        /* the result                      */
  24.     short width, height;    /* dimensions of the picture        */
  25.     char *malloc();
  26.  
  27.     LoadResource(pic);
  28.     HLock((Handle) pic);
  29.     width = (*pic)->picFrame.right - (*pic)->picFrame.left;
  30.     height = (*pic)->picFrame.bottom - (*pic)->picFrame.top;
  31.     HUnlock((Handle) pic);
  32.  
  33.     mapptr = (BitMap *) malloc(sizeof(BitMap));
  34.     SetRect(&mapptr->bounds, 0, 0, width, height);
  35.     mapptr->rowBytes = (width + 7) / 8;
  36.     if (mapptr->rowBytes & 1) {
  37.     ++(mapptr->rowBytes);
  38.     }
  39.     mapptr->baseAddr = (QDPtr) malloc((int) mapptr->rowBytes * height);
  40.  
  41.     GetPort(&saveport);
  42.     OpenPort(&aport);
  43.     SetPortBits(mapptr);
  44.     DrawPicture(pic, &mapptr->bounds);
  45.     SetPort(saveport);
  46.     ClosePort(&aport);
  47.  
  48.     return(mapptr);
  49. }
  50.  
  51. /*
  52.  * randint() - return a random number in the range 0 through range-1.
  53.  *  This routine seeds the number generator on the first call.
  54.  */
  55. int
  56. randint(range)
  57. int range;
  58. {
  59.     static int first = 1;    /* true if this is the first call    */
  60.     long secs;        /* value returned from GetDateTime()    */
  61.  
  62.     if (first) {
  63.     first = 0;
  64.     GetDateTime(&secs);
  65.     randSeed = secs;
  66.     }
  67.     return((int)((unsigned) Random() % range));
  68. }
  69.  
  70. /*
  71.  * setupmemory() - initialize the memory.
  72.  */
  73. setupmemory()
  74. {
  75. #define maxStackSize 8192    /* max size of stack; the heap gets the rest */
  76.  
  77.     typedef long   *lomemptr;    /* a pointer to low memory locations */
  78.  
  79.     lomemptr stackbaseptr;    /* points to current stack base      */
  80.  
  81. /* 
  82.   If you define a GrowZone function to handle bad memory problems,
  83.   you should define it at the top level (not nested), and set it
  84.   here. We don't.
  85. */
  86. /*  SetGrowZone(&mygrowzone);     */
  87.  
  88.  
  89. /* 
  90.   If you needed to use an Application heap limit other than the
  91.   default (which allows 8K for the stack), you'd set it here,
  92.   possible using this technique of explicitly specifying the maximum
  93.   stack size and allocating the rest to the heap.  Should be
  94.   independent of memory size.
  95. */
  96.     stackbaseptr = (lomemptr) 0x908; /* CurStackBase from Tlasm/sysequ.text */
  97.     SetApplLimit((Ptr) (*stackbaseptr - maxStackSize));
  98.  
  99. /* 
  100.   Expand the application heap zone to its maximum size, without
  101.   purging any purgeable resources.  This saves memory compactions
  102.   and heap expansions later.
  103. */
  104.     MaxApplZone();
  105.  
  106. /* 
  107.   get plenty of master pointers now; if we let the Memory Manager
  108.   allocate them as needed, they'd form non-relocatable islands in
  109.   the heap.
  110. */
  111.     MoreMasters();
  112.     MoreMasters();
  113. }
  114.